import {
    dyBtns,
    action
} from './config';
import {
    consoleCustom
} from '@/utils';

/**
 * 关注涨粉
 * @Author yuanyexu
 * @Date 2021-12-03
 */

const guajizhangfen = (params) => {
    // 检查无障碍服务是否已经启用
    auto.waitFor();
    const {
        // 基础配置
        duration,
        browsingStartTime,
        browsingEndTime,
        keyWordSwitch,
        keyWord,
        // 关注
        followSwitch,
        followProb,
        followCountLimitSwitch,
        followCountLimit,
        // 点赞
        giveLikeSwitch,
        giveLikeProb,
        giveLikeCountLimitSwitch,
        giveLikeCountLimit,
        // 点赞限制
        giveLikeLimitSwitch,
        giveLikeLess,
        giveLikeCommentLess,
        // 浏览主页
        homepageSwitch,
        homepageProb,
        // 收藏
        collectSwitch,
        collectProb,
        // 转发
        relaySwitch,
        relayProb,
        // 作品评论内容点赞
        commentGiveLikeSwitch,
        commentGiveLikeMoreThen,
        commentGiveLikeProb,
        // 评论作品
        commentSwitch,
        commentProb,
        commentType,
        commentText,
    } = params;
    const {
        swipeAction,
        sleepAction
    } = action;
    const {
        likeBtn,
        commentBtn,
        relayBtn,
        relayBoxBtn,
        relayPageBtn,
        commentInput,
        followBtn,
        submitCommitBtn,
        hasLiveBtn,
        hasUpdateBtn,
        hasfristInAd,
        getVideoTit,
        commentCount,
        qingshaonian,
        collectFun
    } = dyBtns;
    // 时间参数
    const timeStart = new Date().getTime();
    const timeEnd = timeStart + duration * 60 * 1000;
    // 已关注、点赞
    let followCount = 0;
    let giveLikeCount = 0;
    consoleCustom('挂机涨粉');
    console.info('该脚本适配抖音 version 18.8.0,如遇到执行问题,可在首页提交反馈。');
    launchApp('抖音');
    swipeAction(1000, 2000);
    if (text('允许').findOne(200)) {
        text('允许').click();
    }
    console.info('已打开抖音');
    // 刚进入项目跳过广告
    swipeAction(1000, 2000);
    if (!!hasfristInAd()) {
        hasfristInAd().click();
    }
    // 青少年模式跳过
    if (!!qingshaonian()) {
        qingshaonian().click();
    }
    // 条件限制
    const condition = () => {
        let nowTime = new Date().getTime();
        // 关注上限、点赞上限
        if (followCountLimitSwitch && giveLikeCountLimitSwitch) {
            return nowTime < timeEnd &&
                followCount < followCountLimit &&
                giveLikeCount < giveLikeCountLimit;
        }
        // 关注上限
        if (followCountLimitSwitch) {
            return nowTime < timeEnd &&
                followCount < followCountLimit;
        }
        // 点赞上限
        if (giveLikeCountLimitSwitch) {
            return nowTime < timeEnd &&
                giveLikeCount < giveLikeCountLimit;
        }
        return nowTime < timeEnd;
    }
    let time = 0;
    // 符合条件则运行
    while (condition()) {
        time++;
        console.info(`----------(${time})-----------`);
        const nowTime = new Date().getTime();
        const runMinutes = ~~((nowTime - timeStart) / 60000);
        const runSecond = ~~((nowTime - timeStart) % 60000 / 1000);
        const startInfo = `执行任务${duration}分钟,已运行${runMinutes}分钟${runSecond}秒,已点赞作品${giveLikeCount},已关注作者${followCount}`;
        console.info(startInfo);
        // 抖音更新按钮
        if (!!hasUpdateBtn()) {
            console.info('[更新弹窗]-点击跳过');
            hasUpdateBtn().click();
        }
        // 进入直播间按钮
        if (!!hasLiveBtn()) {
            console.info('[直播视频]-跳过');
            swipeAction();
        }
        // 关键词开关
        if (keyWordSwitch && !!keyWord) {
            if (!getVideoTit()) {
                console.error('[关键词检测]-获取关键词控件异常');
                swipeAction();
                sleepAction(500);
                continue;
            }
            try {
                const videoTit = getVideoTit().text() || '';
                const keyWordGroup = keyWord.split(',');
                const hasKeyWord = keyWordGroup.some(item => videoTit.includes(item));
                // console.info(`[关键词检测]关键词: ${keyWordGroup}`);
                console.info(`[关键词检测]视频文案: ${videoTit}`);
                if (!videoTit) {
                    console.warn('[关键词检测]-无文案视频,跳过');
                    swipeAction();
                    sleepAction(500);
                    continue;
                }
                if (videoTit.slice(-2) === '广告') {
                    console.warn('[关键词检测]-平台广告,跳过');
                    swipeAction();
                    sleepAction(500);
                    continue;
                }
                if (!hasKeyWord) {
                    console.warn(`[关键词检测]-不包含关键词,跳过`);
                    swipeAction();
                    sleepAction(500);
                    continue;
                }
            } catch (error) {
                console.error(error);
                swipeAction();
                sleepAction(500);
                continue;
            }
        }
        // 点赞
        if (giveLikeSwitch) {
            if (!likeBtn()) {
                console.error('[点赞]-点赞控件获取异常');
                return;
            }
            try {
                const randNum = random(0, 100);
                if (giveLikeProb >= randNum) {
                    console.info(`[点赞]-点赞概率:${giveLikeProb}%,随机值(${randNum})命中概率,执行点赞`);
                    likeBtn().click();
                    giveLikeCount++;
                    console.info('[点赞]-已点赞');
                    sleepAction();
                } else {
                    console.warn(`[点赞]-点赞概率:${giveLikeProb}%,随机值(${randNum})未命中概率`);
                }
            } catch (error) {

            }
        }
        // 关注
        if (followSwitch) {
            if (!followBtn()) {
                console.error('[关注]-关注控件获取异常');
                return;
            }
            try {
                const randNum = random(0, 100);
                if (followProb >= randNum) {
                    console.info(`[关注]-关注概率:${followProb}%,随机值(${randNum})命中概率,执行关注`);
                    followBtn().click();
                    followCount++;
                    console.info('[关注]-已关注');
                    sleepAction();
                } else {
                    console.warn(`[关注]-关注概率:${followProb}%,随机值(${randNum})未命中概率`);
                }
            } catch (error) {

            }
        }
        // 评论
        if (commentSwitch) {
            if (!commentBtn()) {
                console.error('[评论]-评论按钮控件获取异常');
                return;
            }
            try {
                !commentCount() ? console.error('[脚本出错]-评论数量控件获取异常') : '';
                const commentC = commentCount();
                console.info(`[评论][视频评论数]-${commentC}`);
                const randNum = random(0, 100);
                if (commentProb >= randNum) {
                    console.info(`[评论]-评论概率:${commentProb}%,随机值(${randNum})命中概率,执行评论`);
                    commentBtn().click();
                    sleepAction(1000, 2000);
                    commentInput().click();
                    sleepAction(1000, 2000);
                    if (commentType === 1) {
                        console.info('[评论][评论方式]-自动仿评论(待完善)');
                        setText('妈妈说玩手机24小时会猝死，我己经玩了23小时59分59秒，怎么可能会犭');
                    }
                    if (commentType === 2 && commentText) {
                        console.info('[评论][评论方式]-自定义评论内容');
                        const commentContents = commentText.split('|');
                        console.log(commentContents);
                        const randNum = random(0, commentContents.length);
                        setText(commentContents[randNum]);
                    }
                    sleepAction(1000, 2000);
                    submitCommitBtn().click();
                    sleepAction(1000, 2000);
                    back();
                    console.info('[评论]-已评论');
                } else {
                    console.warn(`[评论]-评论概率:${relayProb}%,随机值(${randNum})未命中概率`);
                }
            } catch (error) {
                console.error(error);
            }
        }
        // 转发
        if (relaySwitch) {
            if (!relayBtn()) {
                console.error('[转发]-转发控件step1获取异常');
                return;
            }
            try {
                const randNum = random(0, 100);
                if (relayProb >= randNum) {
                    console.info(`转发概率:${relayProb}%,随机值(${randNum})命中概率,执行转发`);
                    relayBtn().click();
                    sleepAction(2000, 3000);
                    if (!relayBoxBtn()) {
                        console.error('[转发]-转发控件step2获取异常');
                        sleepAction(1000, 1500)
                        swipeAction();
                    }
                    console.log(relayBoxBtn())
                    relayBoxBtn().click();
                    sleepAction(2000, 3000);
                    if (!relayPageBtn()) {
                        console.error('[转发]-转发控件step3获取异常');
                        sleepAction(500);
                        swipeAction();
                    }
                    relayPageBtn().click();
                    sleepAction(2000, 3000);
                    console.info('已转发');
                } else {
                    console.warn(`转发概率:${relayProb}%,随机值(${randNum})未命中概率`);
                }
            } catch (error) {
                console.error(error);
            }
        }
        // 浏览主页
        if (homepageSwitch) {
            try {
                const randNum = random(0, 100);
                if (homepageProb >= randNum) {
                    log(`浏览主页概率:${homepageProb}%,随机值(${randNum})命中概率,执行浏览主页`);
                    console.info('待完善');
                } else {
                    console.warn(`浏览主页概率:${homepageProb}%,随机值(${randNum})未命中概率`);
                }
            } catch (error) {
                console.error(error);
            }
        }
        // 收藏
        if (collectSwitch) {
            try {
                const randNum = random(0, 100);
                if (collectProb >= randNum) {
                    log(`收藏概率:${collectProb}%,随机值(${randNum})命中概率,执行收藏`);
                    collectFun();
                } else {
                    console.warn(`收藏概率:${collectProb}%,随机值(${randNum})未命中概率`);
                }
            } catch (error) {
                console.error(error);
            }
        }
        swipeAction();
    }
    const nowTime = new Date().getTime();
    const runMinutes = ~~((nowTime - timeStart) / 60000);
    const runSecond = ~~((nowTime - timeStart) % 60000 / 1000);
    const endInfo = `执行结束：脚本运行${runMinutes ? runMinutes + '分钟' : ''} ${runSecond}秒,点赞作品${giveLikeCount}个,关注作者${followCount}个`;
    console.info(endInfo);
    home();
}
export default guajizhangfen;